<?php

declare(strict_types=1);

namespace app\shop\controller\api\user;

use think\Request;
use think\helper\Str;
use app\shop\controller\api\ShopBase;
use sheep\exception\SheepException;
use app\shop\model\user\Coupon as UserCouponModel;
use app\shop\model\Coupon as CouponModel;

class Coupon extends ShopBase
{

    public function list(Request $request)
    {
        $user = $this->auth()->user();
        $type = $request->param('type', 'can_use');     // 优惠券类型：geted=已领取，can_use=可用，cannot_use=暂不可用，used=已使用，expired=已过期,invalid=已失效（包含已使用和已过期）

        $userCoupons = UserCouponModel::with('coupon')->where('user_id', $user->id);

        if (in_array($type, ['geted', 'can_use', 'cannot_use', 'used', 'expired', 'invalid'])) {
            $userCoupons = $userCoupons->{Str::camel($type)}();
        }

        $userCoupons = $userCoupons->order('id', 'desc')->paginate(request()->param('list_rows', 10));

        return success('获取成功', $userCoupons);
    }
}
